package com.study;

import com.alibaba.druid.pool.DruidDataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.study.config.SpringConfiguration;
import com.study.dao.UserDao;
import com.study.service.UserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ResourceBundle;

public class demo2 {
	@Test
	public void test6(){
		ApplicationContext app =
				new AnnotationConfigApplicationContext(SpringConfiguration.class);
		UserService userService = app.getBean(UserService.class);
		userService.save();
	}
	@Test
	public void test5(){
		ClassPathXmlApplicationContext applicationContext = new
				ClassPathXmlApplicationContext("applicationContext.xml");
		UserService userService = (UserService) applicationContext.getBean("userService1");
		userService.save();
		applicationContext.close();
	}
	@Test
	public void test4() throws SQLException {
		ApplicationContext applicationContext = new
				ClassPathXmlApplicationContext("applicationContext.xml");
		DataSource dataSource = (DataSource) applicationContext.getBean("dateSource1");
		Connection connection = dataSource.getConnection();
		System.out.println(connection);
		connection.close();
	}
	@Test
	public void testC3PO() throws Exception{
		//创建数据源
		ComboPooledDataSource dataSource = new ComboPooledDataSource();

		//设置数据库连接参数
		dataSource.setDriverClass("com.mysql.jdbc.Driver");
		dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test");
		dataSource.setUser("root");
		dataSource.setPassword("root");
		System.out.println(dataSource);
		//获取连接对象
		Connection connection = dataSource.getConnection();
		System.out.println(connection);
	}
	@Test
	public void testDruid() throws Exception{
		//创建数据源
		DruidDataSource dataSource = new DruidDataSource();
		//设置数据库连接参数
		dataSource.setDriverClassName("com.mysql.jdbc.Driver");
		dataSource.setUrl("jdbc:mysql://localhost:3306/test");
		dataSource.setUsername("root");
		dataSource.setPassword("root");
		//获取连接对象
		System.out.println(dataSource);
		Connection connection = dataSource.getConnection();
		System.out.println(connection);
	}

	@Test
	public void test3() throws Exception{
		//读取配置文件
		ResourceBundle rb = ResourceBundle.getBundle("jdbc");
		String driver = rb.getString("jdbc.driver");
		String url = rb.getString("jdbc.url");
		String username = rb.getString("jdbc.username");
		String password = rb.getString("jdbc.password");
		//2、创建数据源对象3、设置数据源的基本连接数据
		ComboPooledDataSource dataSource = new ComboPooledDataSource();

		dataSource.setDriverClass(rb.getString("jdbc.driver"));
		dataSource.setJdbcUrl(rb.getString("jdbc.url"));
		dataSource.setUser(rb.getString("jdbc.username"));
		dataSource.setPassword(rb.getString("jdbc.password"));
//		4、使用数据源获取连接资源和归还连接资源
		Connection connection = dataSource.getConnection();
		System.out.println(connection);
		connection.close();
	}


}
